package com.shunbo.convenient.controller;
import com.shunbo.convenient.service.PictureService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.Picture;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author weizhuang
 * 图片
 */
@RestController
@RequestMapping("/picture")
public class PictureController {


    @Resource
    private PictureService pictureService;

    /**
     * 轮播图列表
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getPictureList(@RequestBody Picture pictures) {
        try {
            List<Picture> all = pictureService.findAll(pictures);

            return ConvenientResult.build(200, "成功", all);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 更新banner
     * @param pictures 更新banner
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updatePicture(@RequestBody Picture pictures){
        try {
            Integer save = pictureService.update(pictures);
            return ConvenientResult.build(200,"成功",save);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }
    /**
     * 添加banner
     * @param pictures 更新banner
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult savePicture(@RequestBody Picture pictures){
        try {
            Integer save = pictureService.save(pictures);
            return ConvenientResult.build(200,"成功",save);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }
    /**
     * 删除banner
     * @param pictures 删除banner
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult delPicture(@RequestBody Picture pictures){
        try {
            Integer save = pictureService.delete(pictures.getId()+"");
            return ConvenientResult.build(200,"成功",save);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }

    /**
     * 首页图
     * @return Result结果集
     */
    @RequestMapping("/index")
    public ConvenientResult getIndex() {
        try {
            Picture picture = new Picture();
            picture.setName("/image/tw.png");
            picture.setPhone("12306");
            return ConvenientResult.build(200, "成功", picture);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }


    /**
     * 尾页图
     * @return Result结果集
     */
    @RequestMapping("/last")
    public ConvenientResult getLast() {
        try {
            Map<String,String> map = new HashMap<>();
            map.put("appName","佳到家");
            map.put("appImg", "/image/index.jpg");
            map.put("appVersion","V_1.0.0");
            return ConvenientResult.build(200, "成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

}
